#!/bin/sh

if [ $# -lt 1 ] ; then
    echo "usage: $(basename $0) PROPS [OPT VAL [...]]" >&2
    exit 2
fi

PROPS="$1"
shift
if [ ! -f "${PROPS}" ] ; then
    echo "${PROPS}: no such file or directory" >&2
    exit 1
fi
DB="$(grep '^db=' $PROPS | sed -e 's/^db=//')"

BEFORE_LOAD="tableCreates"
AFTER_LOAD="indexCreates foreignKeys buildFinish"

for step in ${BEFORE_LOAD} ; do
    ./runSQL.sh "${PROPS}" $step
done

echo "$(date) -- start Load Data..."

startTime=$(date +%Y%m%d-%H:%M:%S)
startTime_s=$(date +%s)

./runLoader.sh "${PROPS}" $*
 

for step in ${AFTER_LOAD} ; do
    ./runSQL.sh "${PROPS}" $step
done

endTime=$(date +%Y%m%d-%H:%M:%S)
endTime_s=$(date +%s)
 
sumTime=$((endTime_s - startTime_s))
echo "$(date) -- End Load Data..."
echo "$startTime ---> $endTime" "Total:$sumTime seconds"
